<?php
	namespace Qzn\Model;
	class Validate {
		/**
		 *    ______ _______  ______ _______ _     _      _____ _______
			 |_____/ |______ |  ____ |______  \___/         |   |______
			 |    \_ |______ |_____| |______ _/   \_      __|__ ______|
	                                                           
			 _     _  _____  _  _  _      _  _  _ _______
			 |_____| |     | |  |  |      |  |  | |______
			 |     | |_____| |__|__|      |__|__| |______
			                                             
			  ______  _____               
			 |_____/ |     | |      |     
			 |    \_ |_____| |_____ |_____
		 */
		
		/**
		 * @param string $string
		 * @param boolean $allow
		 * @return string $html
		 * TODO: http://www.youtube.com/watch?v=p6PZhONZ3Ac
		 */
		public function ValidateString($string, $allow) {
			if ($allow) {
				$string = strip_tags($string, '<a><p><img><h3>');
			} else {
				$string = htmlentities($string);
			}
	
			return $string;
		}
		
		/**
		 * @param string $user
		 * @return boolean
		 */
		public function ValidateUserName($user) {
			$regex = "/^[åäöa-zA-Z0-9@#$%^&+=]{3,36}$/";
			
			return preg_match($regex, $user);
		}
		
		/**
		 * @param string $password
		 * @return booloean
		 */
		public function ValidatePassword($password) {
			$regex = "/^[a-zA-Z0-9@?#$%^&+=]{6,}$/";
			return preg_match($regex, $password);
		}
		
		/**
		 * @param string $number
		 * @return booloean
		 */
		public function ValidateNumber($number) {
			$regex = "/^[-]?[0-9]+$/";
			
			return preg_match($regex, $number);
		}
		
		/**
		 * Vi la till ett bättre format, vi kan själva.
		 * @param string $date
		 * @return booloean
		 */
		public function ValidateDate($date) {
			$regex = "/^([0-9]{4}-[0-9]{2}-[0-9]{2})|([0-9]{2}-[0-9]{2}-[0-9]{2})|([0-9]{6}|([0-9]{8}))$/";
			
			if(preg_match($regex, $date)) {
				switch(strlen($date)) {
					case 10:
							$date = str_replace("-", "", $date);
						break;
					case 8:
							if (strpos($date, "-")) {
								$date = str_replace("-", "", $date);
								if ((int)substr($date, 0, 2) < date("y", time())) {
									$date = "20".$date;
								} else {
									$date= "19".$date;
								}
							}
						break;
					case 6:
							$date = str_replace("-", "", $date);
							if ((int)substr($date, 0, 2) < date("y", time())) {
								$date = "20".$date;
							} else {
								$date= "19".$date;
							}
						break;				
				}
				
				if(checkdate(substr($date, 4, 2), substr($date, 6, 2), substr($date, 0, 4))) {
					return true;
				}
			}
			
			return false;
		}
		
		/**
		 * Vi antar att folk inte reser tillbaka i tiden. Vi antar också att folk vid 1 eller 101 års ålder inte registrerar sig
		 * TODO: Vid tidsresor, ring support.
		 * Code
		 * Wat r u doin
		 * CODE
		 * STAHP
		 * @param string $personNumber
		 * @return boolean
		 */
		public function ValidatePersonNumber($personNumber) {
			$regex = "/^([0-9]{12})|([0-9]{10})|([0-9]{8}-[0-9]{4})|([0-9]{6}-[0-9]{4})$/";
			
			if (preg_match($regex, $personNumber)) {
				switch(strlen($personNumber)) {
					case 10:
							if ((int)substr($personNumber, 0, 2) < date("y", time())) {
								$personNumber = "20".$personNumber;
							} else {
								$personNumber = "19".$personNumber;
							}
						break;
					case 11:
							$personNumber = str_replace("-", "", $personNumber);
							if ((int)substr($personNumber, 0, 2) < date("y", time())) {
								$personNumber = "20".$personNumber;
							} else {
								$personNumber = "19".$personNumber;
							}
						break;
					case 13:
							$personNumber = str_replace("-", "", $personNumber);
						break;				
				}
				
				if (checkdate(substr($personNumber, 4, 2), substr($personNumber, 6, 2), substr($personNumber, 0, 4))) {
					$testNumber = 0;
					for ($i = 2; $i < strlen($personNumber); $i += 1) {
						$tempNumber = (int)substr($personNumber, $i, 1)*($i%2 == 0 ? 2 : 1);
						$testNumber += (strlen($tempNumber) == 2 ? ((int)substr($tempNumber, 0, 1) + (int)substr($tempNumber, 1, 1)) : $tempNumber);
					}
					
					if ($testNumber%10 == 0) {
						return true;
					}
				}
			}
			
			return false;
		}
		
		/**
		 * @param string $email
		 * @return booloean
		 */
		public function ValidateEmail($email) {
			$regex = "/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i";
			
			return preg_match($regex, $email);
		}
		
		/**
		 * Test data
		 * @return string test data
		 */
		public function Test() {
			
			$email = "testa mig";
			
			if ($this->ValidateEmail($email)) {
				return "testa mig funkar inte";
			}
			
			$email = "test@sad";
			
			if ($this->ValidateEmail($email)) {
				return "test@sad funkar inte";
			}
			
			$email = "test@sad.se";
			
			if (!$this->ValidateEmail($email)) {
				return "test@sad.se valideras skumt";
			}
			
			$personNumber = "123";
			
			if ($this->ValidatePersonNumber($personNumber)) {
				return "123 är inte ett korrekt personnummer (detta var ett fel)";
			}
	
			$personNumber = "aaaaaaaadddd";
			
			if ($this->ValidatePersonNumber($personNumber)) {
				return "aaaaaaaadddd är fel";
			}
			
			$personNumber = "91p122-adas";
			
			if ($this->ValidatePersonNumber($personNumber)) {
				return "91p122-adas är fel";
			}
				
			$personNumber = "911225-0379";
			
			if (!$this->ValidatePersonNumber($personNumber)) {
				return "Jag är rätt och fungerar inte";
			}
			
			$personNumber = "9112250379";
			
			if (!$this->ValidatePersonNumber($personNumber)) {
				return "9112250379 fungerar inte!";
			}
			
			$personNumber = "19911225-0379";
			
			if (!$this->ValidatePersonNumber($personNumber)) {
				return "19911225-0379 fungerar inte.";
			}
			
			$personNumber = "199112250379";
			
			if (!$this->ValidatePersonNumber($personNumber)) {
				return "199112250379 fungerar inte.";
			}
			
			$date = "feldatum";
			
			if($this->ValidateDate($date)) {
				return "feldatum valideras fel.";
			}
			
			$date = "123123";
			
			if($this->ValidateDate($date)) {
				return "123123 valideras fel.";
			}
			
			$date = "1999-10-01";
			
			if(!$this->ValidateDate($date)) {
				return "1999-10-01 valideras fel.";
			}
			
			$date = "99-10-01";
			
			if(!$this->ValidateDate($date)) {
				return "99-10-01 valideras fel.";
			}
			
			$date = "991001";
			
			if(!$this->ValidateDate($date)) {
				return "991001 valideras fel.";
			}
			
			$date = "19991001";
			
			if(!$this->ValidateDate($date)) {
				return "19991001 valideras fel.";
			}
			
			$date = "HejAlvinNöjdNu?";
			
			if($this->ValidateDate($date)) {
				return "HejAlvinNöjdNu? valideras fel.";
			}
			
			$date = "abcdefkaje";
			
			if($this->ValidateDate($date)) {
				return "abcdefkaje valideras fel.";
			}
			
			$number = "quizärful";
			
			if($this->ValidateNumber($number)) {
				return "validatenumber är trasig.";
			}
			
			$number = "123123";
			
			if(!$this->ValidateNumber($number)) {
				return "validatenumber är trasig.";
			}
			
			$number = "quizärfuuuu3";
			
			if($this->ValidateNumber($number)) {
				return "validatenumber är trasig.";
			}
			
			$password = "ingenting";
			
			if(!$this->ValidatePassword($password)) {
				return "Ah men de funkar. (password)";
			}
			
			$password = "snor";
			
			if($this->ValidatePassword($password)) {
				return "Fast så gjorde det inte det. (password)";
			}
			
			if(!$this->ValidateString("blahblahblahtestaentaggtyp", true)) {
				return "didntwork";
			}
			
			if(!$this->ValidateString("<h2>testar</h2>", false)) {
				return "funkar inte?";
			}
			
			return true;
		}
	}
?>